import { asyncRouterMap, constantRouterMap } from '@/router/index';

//判断是否有权限访问该菜单
function hasPermission(menus, route) {
  // console.log('返回所有菜单1', menus, route);
  if (route.name) {
    let currMenu = getMenu(route.name, menus);
    // console.log('shujuchushi', currMenu, route);
    if (currMenu!=null) {
      //设置菜单的标题、图标和可见性
      if (currMenu.title != null && currMenu.title !== '') {
        route.meta.title = currMenu.title;
      }
      if (currMenu.icon != null && currMenu.title !== '') {
        route.meta.icon = currMenu.icon;
      }
      if(currMenu.hidden!=null){
        // route.hidden = currMenu.hidden !== 0;
        route.hidden = currMenu.hidden;
      }
      if (currMenu.sort != null && currMenu.sort !== '') {
        route.sort = currMenu.sort;
      }
      return true;
    } else {
      route.sort = 0;
      if (route.hidden !== undefined && route.hidden === true) {
        route.sort=-1;
        // console.log(2222, route);
        return true;
      } else if (route.path === 'systemList' || route.path === 'playManage') {
        route.hidden = false;
        return true;
      } else {
        return false;
      }
    }
  } else {
    return true
  }
}

//根据路由名称获取菜单
function getMenu(name, menus) {
  for (let i = 0; i < menus.length; i++) {
    let menu = menus[i];
    if (name===menu.name) {
      return menu;
    }
  }
  return null;
}

//对菜单进行排序
function sortRouters(accessedRouters) {
  for (let i = 0; i < accessedRouters.length; i++) {
    let router = accessedRouters[i];
    if(router.children && router.children.length > 0){
      router.children.sort(compare("sort"));
    }
  }
  accessedRouters.sort(compare("sort"));
}

//降序比较函数
function compare(p){
  return function(m,n){
    let a = m[p];
    let b = n[p];
    return b - a;
  }
}
// 根据权限处理页面路由配置
function dealRouter(routers, constantRouterMap, state) {
  if (routers.some(item => item.path === "/system")) {
    const dom = routers.filter(item => item.path === '/system');
    state.defaulRoletUrl = '/system/' + dom[0].children[0].path;
  } else if (routers.some(item => item.path === "/play")) {
    const dom = routers.filter(item => item.path === '/play');
    state.defaulRoletUrl = '/play/' + dom[0].children[0].path;
  } else if (routers.some(item => item.path === "/ums")) {
    const dom = routers.filter(item => item.path === '/ums');
    state.defaulRoletUrl = '/ums/' + dom[0].children[0].path;
  }

  if (routers.some(item => item.path === '/user')) {
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/audit")) {
    const dom = routers.filter(item => item.path === '/audit');
    constantRouterMap[2].redirect = '/audit/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/content")) {
    const dom = routers.filter(item => item.path === '/content');
    constantRouterMap[2].redirect = '/content/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/activity")) {
    const dom = routers.filter(item => item.path === '/activity');
    constantRouterMap[2].redirect = '/activity/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/ticket")) {
    const dom = routers.filter(item => item.path === '/ticket');
    constantRouterMap[2].redirect = '/ticket/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/order")) {
    const dom = routers.filter(item => item.path === '/order');
    constantRouterMap[2].redirect = '/order/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/refund")) {
    const dom = routers.filter(item => item.path === '/refund');
    constantRouterMap[2].redirect = '/refund/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/versions")) {
    const dom = routers.filter(item => item.path === '/versions');
    constantRouterMap[2].redirect = '/versions/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/document")) {
    const dom = routers.filter(item => item.path === '/document');
    constantRouterMap[2].redirect = '/document/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/system")) {
    const dom = routers.filter(item => item.path === '/system');
    constantRouterMap[2].redirect = '/system/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/play")) {
    const dom = routers.filter(item => item.path === '/play');
    constantRouterMap[2].redirect = '/play/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  } else if (routers.some(item => item.path === "/ums")) {
    const dom = routers.filter(item => item.path === '/ums');
    constantRouterMap[2].redirect = '/ums/' + dom[0].children[0].path;
    constantRouterMap[2].children = dom[0].children;
    return constantRouterMap;
  }
}
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
    defaultUrl: '',
    defaulRoletUrl: ''
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      const a = dealRouter(routers, constantRouterMap, state);
      constantRouterMap[2] = a[2];
      state.defaultUrl = a[2].redirect;
      state.routers = constantRouterMap.concat(routers);
      // console.log(routers, constantRouterMap, state);
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        const { menus } = data;
        const { username } = data;
        const accessedRouters = asyncRouterMap.filter(v => {
          //admin帐号直接返回所有菜单
          // if(username==='admin') return true;
          // console.log('所有菜单', v);
          if (hasPermission(menus, v)) {
            if (v.children && v.children.length > 0) {
              v.children = v.children.filter(child => {
                // console.log(child);
                if (hasPermission(menus, child)) {
                  // console.log(2222,child);
                  return child
                }
                return false;
              });
              // console.log('返回所有菜单1', menus, v);
              return v
            } else {
              // console.log('返回所有菜单2', menus, v);
              return v
            }
          }
          return false;
        });
        //对菜单进行排序
        sortRouters(accessedRouters);
        commit('SET_ROUTERS', accessedRouters);
        resolve();
      })
    }
  }
};

export default permission;

